<template>
  <div class="login-container" :style="{ 'background': 'url(' + globalConfig.logo + ') no-repeat', 'background-size': '100% 100%' }">
    <div class="login-weaper animated bounceInDown">
      <div class="login-title">{{ globalConfig.name }}医务一网通办</div>
      <div class="login-border">
        <div class="login-main">
          <div class="login-des">All in one medical service</div>
          <!-- <div class="login-tab"><div class="tab">账号密码登陆</div></div> -->
          <div class="form-content">
            <div class="login-subtitle">账号密码登陆</div>
            <el-form
              ref="loginForm"
              :model="loginForm"
              :rules="loginRules"
              class="login-form"
              autocomplete="on"
              label-position="left"
            >
              <el-form-item prop="username">
                <span class="svg-container">
                  <i class="el-icon-user" />
                </span>
                <el-input
                  ref="username"
                  v-model="loginForm.username"
                  placeholder="用户名"
                  name="username"
                  type="text"
                  tabindex="1"
                  autocomplete="on"
                />
              </el-form-item>

              <el-tooltip
                v-model="capsTooltip"
                content="Caps lock is On"
                placement="right"
                manual
              >
                <el-form-item prop="password">
                  <span class="svg-container">
                    <svg-icon icon-class="password" />
                  </span>
                  <el-input
                    :key="passwordType"
                    ref="password"
                    v-model="loginForm.password"
                    :type="passwordType"
                    placeholder="密码"
                    name="password"
                    tabindex="2"
                    autocomplete="on"
                    @keyup.native="checkCapslock"
                    @blur="capsTooltip = false"
                    @keyup.enter.native="handleLogin"
                  />
                  <span class="show-pwd" @click="showPwd">
                    <svg-icon
                      :icon-class="
                        passwordType === 'password' ? 'eye' : 'eye-open'
                      "
                    />
                  </span>
                </el-form-item>
              </el-tooltip>
              <template v-if="isVerifyCodeTmp">
                <el-form-item
                  prop="code"
                  style="width: 66%;float: left; margin-bottom: 13px"
                >
                  <span class="svg-container">
                    <svg-icon icon-class="aq" />
                  </span>
                  <el-input
                    ref="username"
                    v-model="loginForm.code"
                    placeholder="验证码"
                    name="username"
                    type="text"
                    tabindex="3"
                    maxlength="5"
                    autocomplete="off"
                    style=" width: 75%;"
                    @keyup.enter.native="handleLogin"
                  />
                </el-form-item>
                <div
                  class="login-code"
                  style="cursor:pointer; width: 30%;height: 44px;float: right;background-color: #f0f1f5;"
                >
                  <img
                    style="height: 44px;width: 100%;border: 1px solid rgba(0,0,0, 0.1);border-radius:5px;"
                    :src="codeUrl"
                    @click="getNewCode"
                  >
                </div>
              </template>
              <!-- <div
                prop="code"
                style="width: 100%;float: left;margin-bottom: 13px"
              >
                <el-checkbox v-model="isLdapTmp">自动登陆</el-checkbox>
              </div> -->
              <el-button
                :loading="loading"
                type="primary"
                style="width:100%;padding:14px 20px;margin-bottom:30px;margin-top:30px;border-radius:8px"
                @click.native.prevent="handleLogin"
              >
                <span v-if="!loading">登 录</span>
                <span v-else>登 录 中...</span>
              </el-button>
            </el-form>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getCodeImg, getNewCodeImg } from '@/api/login'
import moment from 'moment'
import { mapGetters } from 'vuex'
import { encryptPassword } from '@/utils/jsencrypt'
export default {
  name: 'LoginIndex',
  data() {
    return {
      isLdapTmp: false,
      codeUrl: '',
      cookiePassword: '',
      loginForm: {
        username: '',
        password: '',
        rememberMe: false,
        code: '',
        uuid: '',
        loginType: 1
      },
      realCode: '',
      activeName: 'first',
      loginRules: {
        username: [
          { required: true, trigger: 'blur', message: '用户名不能为空' }
        ],
        password: [
          { required: true, trigger: 'blur', message: '密码不能为空' }
        ],
        code: [{ required: true, trigger: 'change', message: '验证码不能为空' }]
      },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      redirect: undefined,
      otherQuery: {},
      currentTime: null
    }
  },
  computed: {
    ...mapGetters(['title', 'logo', 'isLdap', 'isVerifyCode', 'globalConfig']),
    isVerifyCodeTmp: function() {
      if (this.isVerifyCode) {
        // this.getCode()
        this.getNewCode()
      }
      return this.isVerifyCode
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    },
    isLdap: {
      handler: function(val) {
        this.isLdapTmp = val
      }
    },
    isVerifyCode: {
      handler: function(val) {
        if (val) {
          // this.getCode()
          this.getNewCode()
        }
      }
    }
  },
  created() {
    // window.addEventListener('storage', this.afterQRScan)
    this.getCurrentTime()
  },
  mounted() {
    if (this.loginForm.username === '') {
      this.$refs.username.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
  },
  destroyed() {
    clearInterval(this.timer)
    // window.removeEventListener('storage', this.afterQRScan)
  },
  methods: {
    handleClick(tab, event) {
      console.log(tab, event)
    },
    getCurrentTime() {
      this.timer = setInterval(_ => {
        this.currentTime = moment().format('YYYY-MM-DD HH时mm分ss秒')
      }, 1000)
    },
    getCode() {
      getCodeImg().then(res => {
        if (res !== undefined) {
          this.codeUrl = res.data
          this.loginForm.uuid = res.id
        }
      })
    },
    getNewCode() {
      getNewCodeImg().then(res => {
        console.log(res)
        this.codeUrl = 'data:image/git;base64,' + res.records[0].data
        console.log(3333, this.codeUrl)
        // this.loginForm.uuid = res.id
        this.realCode = res.records[0].code
      })
    },
    checkCapslock({ shiftKey, key } = {}) {
      if (key && key.length === 1) {
        this.capsTooltip =
          (shiftKey && (key >= 'a' && key <= 'z')) ||
          (!shiftKey && (key >= 'A' && key <= 'Z'))
      }
      if (key === 'CapsLock' && this.capsTooltip === true) {
        this.capsTooltip = false
      }
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid && this.realCode === this.loginForm.code) {
          if (this.isLdapTmp) {
            // this.loginForm.loginType = 1;
            this.loginForm.loginType = 0
          } else {
            this.loginForm.loginType = 0
          }
          this.loading = true
          /* if(this.realCode===this.loginForm.code){

          }*/
          this.$store.dispatch('user/newLogin', { 'loginType': '0', 'userID': this.loginForm.username, 'password': this.loginForm.password }).then(() => {
            this.$router.push({ path: '/' })
            // window.location.reload()
            this.loading = false
          }).catch(() => {
            this.loading = false
            // this.getCode()
            this.getNewCode()
          })
          // const loginFormTmp = { ...this.loginForm }
          // loginFormTmp.username = 'ywk'
          // loginFormTmp.password = 'Qwer@1234'
          // loginFormTmp.password = encryptPassword(loginFormTmp.password)
          // this.$store
          //   .dispatch('user/login', loginFormTmp)
          //   .then(() => {
          //   })
          //   .catch(() => {
          //     this.loading = false
          //     // this.getCode()
          //   })
        } else {
          this.$message({
            message: '用户名，密码或验证码不对，请检查。',
            type: 'error'
          })
          return false
        }
      })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    }
  }
}
</script>

<style lang="scss" scoped>
$bg: #fff;
$light_gray: #fff;
$cursor: #000;

.login-container {
    display: flex;
    width: 100%;
    height: 100%;
    margin: 0 auto;
    /* background: url("~@/assets/login-bg2.png") no-repeat; */
    background-color: #f0f2f5;
    position: relative;
    background-size: 100% 100%;
    background-attachment: fixed;
}

.login-weaper {
    margin: 0 auto;
    width: 600px;
    margin-top: 130px;
    z-index: 1000;
}

.login-border {
    position: relative;
    min-height: 500px;
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    border-left: none;
    border-top-right-radius: 5px;
    border-bottom-right-radius: 5px;
    color: #fff;
    width: 100%;
    float: left;
}

.login-main {
    margin: 0 auto;
    width: 75%;
}

.form-content {
    padding: 30px;
    background-color: rgba($color: #ffffff, $alpha: 0.8);
    border-radius: 16px;
    margin-top: 30px;
}

.login-title {
    color: #fff;
    margin-bottom: 10px;
    font-weight: bold;
    font-size: 36px;
    text-align: center;
}

.login-des {
    color: #fff;
    margin-bottom: 10px;
    font-size: 20px;
    text-align: center;
}

.login-subtitle {
    color: #666;
    margin-bottom: 30px;
    font-size: 22px;
    text-align: center;
    font-weight: bold;
}

.login-tab {
    color: #1890ff;
    width: 100%;
    text-align: center;
    margin-bottom: 20px;
    display: flex;
    font-size: 15px;
    justify-content: center;
    align-items: center;
    .tab {
        width: 130px;
        padding: 10px;
        border-bottom: 2px solid #1890ff;
        cursor: pointer;
    }
}

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
    .login-container .el-input input {
        color: $cursor;
    }
}

/* reset element-ui css */
.login-container {
    ::v-deep .el-input {
        display: inline-block;
        height: 40px;
        width: 85%;

        input {
            background: transparent;
            border: 0px;
            -webkit-appearance: none;
            border-radius: 0px;
            padding: 12px 5px 12px 15px;
            color: #333;
            height: 40px;
            caret-color: #111;

            &::-webkit-input-placeholder {
                color: #333;
            }
        }
    }

    .el-form-item {
        border: 1px solid rgba(0, 0, 0, 0.6);
        background: transparent;
        border-radius: 5px;
        color: #111;
    }
}
$bg: #2d3a4b;
$dark_gray: #111;
$light_gray: #eee;

.login-container {
    .tips {
        font-size: 14px;
        color: #fff;
        margin-bottom: 10px;

        span {
            &:first-of-type {
                margin-right: 16px;
            }
        }
    }

    .svg-container {
        padding: 3px 5px 3px 15px;
        color: $dark_gray;
        vertical-align: middle;
        width: 30px;
        display: inline-block;
    }

    .title-container {
        position: relative;

        .title {
            font-size: 26px;
            color: $light_gray;
            margin: 0px auto 40px auto;
            text-align: center;
            font-weight: bold;
        }
    }

    .show-pwd {
        position: absolute;
        right: 10px;
        top: 7px;
        font-size: 16px;
        color: $dark_gray;
        cursor: pointer;
        user-select: none;
    }

    .thirdparty-button {
        position: absolute;
        right: 0;
        bottom: 6px;
    }

    @media only screen and (max-width: 470px) {
        .thirdparty-button {
            display: none;
        }
    }
}
</style>
